home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / mint / mntlib43 / mntlib / fopen.c < prev    next >
C/C++ Source or Header  |  1994-01-15  |  3KB  |  161 lines

  1. /* from Dale Schumacher's dLibs */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <fcntl.h>
  6. #include <unistd.h>
  7. #include <errno.h>
  8.  
  9. __EXTERN void _getbuf __PROTO((FILE *));
  10. static FILE *_fopen __PROTO((const char *, const char *, FILE *));
  11.  
  12. extern int __mint;
  13.  
  14. /* lowest character device handle # */
  15. #define    LAST_DEVICE    __SMALLEST_VALID_HANDLE
  16.  
  17. extern int __default_mode__;
  18.  
  19. static FILE *_fopen(filename, mode, fp)
  20.     const char *filename;
  21.     const char *mode;
  22.     FILE *fp;
  23. /*
  24.  *    INTERNAL FUNCTION.  Attempt to open <filename> in the given
  25.  *    <mode> and attach it to the stream <fp>
  26.  */
  27.     {
  28.     register int h, i, iomode = 0, f = __default_mode__;
  29.  
  30.     while(*mode)
  31.         {
  32.         switch(*mode++)
  33.             {
  34.             case 'r':
  35.                 f |= _IOREAD;
  36.                 break;
  37.             case 'w':
  38.                 f |= _IOWRT;
  39.                 iomode |= (O_CREAT | O_TRUNC);
  40.                 break;
  41.             case 'a':
  42.                 f |= _IOWRT;
  43.                 iomode |= (O_CREAT | O_APPEND);
  44.                 break;
  45.             case '+':
  46.                 f &= ~(_IOREAD | _IOWRT);
  47.                 f |= _IORW;
  48.                 break;
  49.             case 'b':
  50.                 f |= _IOBIN;
  51.                 break;
  52.             case 't':
  53.                 f &= ~_IOBIN;
  54.                 break;
  55.             default:
  56.                 return(NULL);
  57.             }
  58.         }
  59.     if((i = (f & (_IORW | _IOREAD | _IOWRT))) == 0)
  60.         return(NULL);
  61.     else if(i == _IOREAD)
  62.         iomode |= O_RDONLY;
  63.     else if(i == _IOWRT)
  64.         iomode |= O_WRONLY;
  65.     else
  66.         iomode |= O_RDWR;
  67.     iomode |= O_NOCTTY;
  68.     h = open(filename, iomode, 0666);
  69.     if(h < __SMALLEST_VALID_HANDLE)
  70.         {
  71.         return(NULL);        /* file open/create error */
  72.         }
  73.     if(isatty(h))
  74.         f |= __mint ? (_IODEV | _IONBF| _IOBIN) : (_IODEV | _IONBF);
  75.     else
  76.         f |= _IOFBF;
  77.     fp->_file = h;            /* file handle */
  78.     fp->_flag = f;            /* file status flags */
  79.     if (iomode & O_APPEND)
  80.         (void) fseek(fp, 0L, SEEK_END);
  81.  
  82.     return(fp);
  83.     }
  84.  
  85. FILE *fopen(filename, mode)
  86.     const char *filename, *mode;
  87.     {
  88.     register int i;
  89.     register FILE *fp = NULL;
  90.  
  91.     for(i=0; (!fp && (i < _NFILE)); ++i)
  92.         if(!(_iob[i]._flag & (_IORW | _IOREAD | _IOWRT))) 
  93.             fp = &_iob[i]; /* empty slot */
  94.     if(fp != NULL)
  95.     {
  96.         if(_fopen(filename, mode, fp) == NULL)
  97.             return NULL;
  98.         _getbuf(fp);
  99.         return fp;
  100.     }
  101.     else
  102.       {
  103.         errno = EMFILE;
  104.         return NULL;
  105.       }
  106.     }
  107.  
  108. /*
  109.  * re-coded,  foobared code deleted
  110.  *
  111.  *    ++jrb
  112.  */
  113. FILE *freopen(filename, mode, fp)
  114.     const char *filename, *mode;
  115.     FILE *fp;
  116. {
  117.     unsigned int f;
  118.     
  119.     if(fp == NULL) return NULL;
  120.     
  121.     f = fp->_flag;
  122.     if((f & (_IORW | _IOREAD | _IOWRT)) != 0)
  123.     { /* file not closed, close it */
  124. #if 0
  125.         if(fflush(fp) != 0) return NULL;            /* flush err */
  126.         if(close(fp->_file) != 0) return NULL;        /* close err */
  127. #else
  128.     fflush(fp);                /* ANSI says ignore errors */
  129.     if (__mint || !(f & _IODEV))        /* leave tty's alone */
  130.       close(fp->_file);
  131. #endif
  132.     }
  133.     /* save buffer discipline and setbuf settings, and _IOWRT just for
  134.        determinining line buffering after the next _fopen */
  135.     f &= (_IOFBF | _IOLBF | _IONBF | _IOMYBUF | _IOWRT);
  136.  
  137.     /* open the new file according to mode */
  138.     if((fp = _fopen(filename, mode, fp)) == NULL)
  139.     return NULL;
  140.     if(fp->_flag & _IODEV)
  141.     { /* we are re-opening to a tty */
  142.     if((f & _IOFBF) && (f & _IOWRT) && (f & _IOMYBUF))
  143.     {   /* was a FBF & WRT & !setvbuff'ed  */
  144.         /* reset to line buffering */
  145.         f &= ~_IOFBF;
  146.         f |=  _IOLBF;
  147.     }
  148.     }
  149.     f &= ~_IOWRT; /* get rid of saved _IOWRT flag */
  150.     
  151.     /* put buffering and discipline flags in new fp->_flag */
  152.     fp->_flag &= ~(_IONBF | _IOLBF | _IOFBF | _IOMYBUF);
  153.     fp->_flag |= f;
  154.     
  155.     if (fp->_base == NULL)
  156.       /* get new buffer if file was closed */
  157.       _getbuf (fp);
  158.  
  159.     return fp;
  160.     }
  161.